// This module is licensed and may be used under the terms of the
//  GPL, GNU General Public License, V3 or later, http://www.gnu.org/licenses/gpl.html
//
// Please contact the author if you need another license.
// This module is provided "as is", without warranties of any kind.

import java.text.*;
import java.io.*;

/**
 * A simple SMTP client module
 * 
 * Project home page: http://code.google.com/p/simple-net-comm/
 * Author: Jonathan Bonazza
 * License: GPL
 */
public class SmtpNetClient extends DefaultNetComm
{

	public SmtpNetClient(String uid, String addr, int port) throws IOException
	{
		super(uid, addr, port);
	}

	public void login(String username, String pass) throws IOException, Exception
	{
		sendMessage("HELO");
		expect("250");
		sendMessage("AUTH LOGIN");
		expect("334");
		sendMessage(new String(Base64Coder.encodeString(username)));
		expect("334");
		sendMessage(new String(Base64Coder.encodeString(pass)));
		expect("235");
	}

	public void sendEmailMessage(EmailMessage msg) throws IOException, Exception
	{
		sendMessage("MAIL FROM: " + msg.getFrom());
		expect("250");
		sendMessage("RCPT TO: " + msg.getTo());
		expect("250");
		sendMessage("DATA");
		expect("354");
		sendMessage(msg.getSubject());

		SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yy hh:mm:ss");
		String dateStr = sdf.format(msg.getDate());

		sendMessage(dateStr);
		sendMessage(msg.getFrom());
		sendMessage(msg.getTo());
		sendMessage("");
		sendMessage(msg.getBody());
		sendMessage(".");
	}
}
